#include"../file_h/SDL2.h"
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
TTF_Font *font = NULL;
SDL_Texture *img_texture = NULL;

bool load_assets() {
    // 只加载一次你想替代数字1的图片，比如 "img/1.png"
    SDL_Surface *img_surface = IMG_Load("../image.png");
    if (!img_surface) {
        printf("IMG_Load Error: %s\n", IMG_GetError());
        return false;
    }
    img_texture = SDL_CreateTextureFromSurface(renderer, img_surface);
    SDL_FreeSurface(img_surface);
    if (!img_texture) {
        printf("SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError());
        return false;
    }
    return true;
}

void free_assets() {
    if (img_texture) SDL_DestroyTexture(img_texture);
}


bool init() {
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL_Init Error: %s\n", SDL_GetError());
        return false;
    }
    if(TTF_Init() == -1){
        printf("TTF_Init Error: %s\n", TTF_GetError());
        return false;
    }
    //创建视窗
    window = SDL_CreateWindow("2048 SDL2 Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if(!window){
        printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
        return false;
    }
    // 视窗渲染器
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if(!renderer){
        printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
        return false;
    }

        if (!load_assets()) {
        return false;
    }

    font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 48);
    if(!font){
        printf("TTF_OpenFont Error: %s\n", TTF_GetError());
        return false;
    }
    return true;
}

void cleanup() {
    free_assets();
    if(font) TTF_CloseFont(font);
    if(renderer) SDL_DestroyRenderer(renderer);
    if(window) SDL_DestroyWindow(window);
    TTF_Quit();
    SDL_Quit();
}

// 颜色选择，根据数字不同颜色
SDL_Color get_color(int val){
    switch(val){
        case 2: return (SDL_Color){238, 228, 218, 255};
        case 4: return (SDL_Color){237, 224, 200, 255};
        case 8: return (SDL_Color){242, 177, 121, 255};
        case 16: return (SDL_Color){245, 149, 99, 255};
        case 32: return (SDL_Color){246, 124, 95, 255};
        case 64: return (SDL_Color){246, 94, 59, 255};
        case 128: return (SDL_Color){237, 207, 114, 255};
        case 256: return (SDL_Color){237, 204, 97, 255};
        case 512: return (SDL_Color){237, 200, 80, 255};
        case 1024: return (SDL_Color){237, 197, 63, 255};
        case 2048: return (SDL_Color){237, 194, 46, 255};
        default: return (SDL_Color){205, 193, 180, 255};
    }
}

// 渲染单个格子和数字
void render_cell(int i, int j, int val){
    SDL_Rect rect = {
        GRID_START_X + (j-1)*CELL_SIZE,
        GRID_START_Y + (i-1)*CELL_SIZE,
        CELL_SIZE - 10,
        CELL_SIZE - 10
    };
    SDL_Color bg = get_color(val);
    SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a);
    SDL_RenderFillRect(renderer, &rect);

    
// 修改 render_cell 函数中 if(val > 0) 的部分如下：
if (val > 0) {
    if (val == 1 && img_texture) {
        // 显示图片
        SDL_RenderCopy(renderer, img_texture, NULL, &rect);
    } else {
        // 显示文字
        char num_text[10];
        sprintf(num_text, "%d", val);
        SDL_Color textColor = {119, 110, 101, 255};
        SDL_Surface *textSurface = TTF_RenderText_Blended(font, num_text, textColor);
        SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

        int textW, textH;
        SDL_QueryTexture(textTexture, NULL, NULL, &textW, &textH);
        SDL_Rect textRect = {
            rect.x + (rect.w - textW) / 2,
            rect.y + (rect.h - textH) / 2,
            textW, textH
        };

        SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
        SDL_FreeSurface(textSurface);
        SDL_DestroyTexture(textTexture);
    }
}
}

// 渲染分数
void render_score(int score){
    // 定义一个字符数组，用于存储分数文本
    char score_text[20];
    // 将分数转换为字符串，并存储在score_text中
    sprintf(score_text,"score: %d",score);
    // 定义一个颜色，用于设置文本颜色
    SDL_Color textColor = {255, 255, 255, 255};
    // 使用TTF_RenderText_Blended函数将文本转换为SDL_Surface
    SDL_Surface *textSurface = TTF_RenderText_Blended(font,score_text,textColor);
    // 使用SDL_CreateTextureFromSurface函数将SDL_Surface转换为SDL_Texture
    SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer,textSurface);
    int textW, textH;
    SDL_QueryTexture(textTexture, NULL, NULL, &textW, &textH);
    SDL_Rect textRect = {
        WINDOW_WIDTH - textW - 20,20,textW,textH
    };

    SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
    SDL_FreeSurface(textSurface);
    SDL_DestroyTexture(textTexture);
}

void render_max_score(int max_score){
    char max_score_text[20];
    sprintf(max_score_text,"max_score:%d",max_score);
    SDL_Color textColor = {255,255,255,255};
    SDL_Surface *textSurface= TTF_RenderText_Blended(font,max_score_text,textColor);
    SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer,textSurface);
    int textW, textH;
    SDL_QueryTexture(textTexture, NULL, NULL, &textW, &textH);
    SDL_Rect textRect = {
        WINDOW_WIDTH - textW - 20, 60, textW, textH
    };
    SDL_RenderCopy(renderer,textTexture, NULL, &textRect);
    SDL_FreeSurface(textSurface);
    SDL_DestroyTexture(textTexture);
}


void render_grid(){
    SDL_SetRenderDrawColor(renderer, 187, 173, 160, 255); // 背景色
    SDL_RenderClear(renderer);

    for(int i=1; i<=4; i++){
        for(int j=1; j<=4; j++){
            render_cell(i, j, arr[i][j]);
        }
    }

    render_score(score); // 渲染分数
    render_max_score(max_score);
    SDL_RenderPresent(renderer);
}
